// packageA/pages/shopDetail/index.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
        shopData:{},
        salesPrice:0,
        orderInfo:{
          spuId: "",
          quantity: 1,
          paymentPrice: 0,
          size:""
        }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      // console.log(options.id)
      this.getShops(options.id)
  },
  async getShops(id){
    let res = await getApp().api.getShopdetail(id)
    // console.log(res)
    if(res.code!=200)return
    this.setData({
        shopData:res.data,
        ['orderInfo.spuId']:res.data.id,
        ['orderInfo.paymentPrice']:res.data.salesPrice,
        salesPrice:res.data.salesPrice,
      })
  },
  // 数量变化
  numChange(e){
      // console.log(e)
      this.setData({
        ['orderInfo.quantity']:e.detail,
        ['orderInfo.paymentPrice']:parseFloat((this.data.salesPrice*e.detail).toFixed(2))
      })
      // console.log(this.data.orderInfo)
  },
  //选择尺寸
  sizeTap(e){
    // console.log(e.currentTarget.id)
    this.setData({
      ['orderInfo.size']:e.currentTarget.id,
    })
  },
   //确定下单
   async surePlace(){
     // console.log(1)
     let dataInfo = {
      'skus':[
        this.data.orderInfo
      ]
    }
    // console.log(dataInfo)
    let res = await getApp().api.placeOrder(dataInfo)
    // console.log(res)
    if(res.code==500)return wx.showToast({
      title: '出错了！',
      icon:"error"
    })
    if(res.code==506)return wx.showToast({
      title: '库存不足',
      icon:"error"
    })
    if(res.code==508)return Dialog.confirm({
      title: '提示',
      message: '未添加地址！是否前往添加？',
    }).then(() => {
      wx.navigateTo({
        url: '/packageB/pages/address/index',
      })
    })
    .catch(() => {
      // on cancel
    }); 
    Dialog.confirm({
      title: '提示',
      message: '下单成功！前往支付',
    })
      .then(() => {
        wx.navigateTo({
          url: '/packageB/pages/orderDetail/index?status=1&id='+res.data,
        })
      })
      .catch(() => {
        // on cancel
      });
    
   },
     //收藏
  async collectTo(){
   if(this.data.shopData.shoppingCart){//取消收藏
        let result = await getApp().api.clearCollect([this.data.shopData.id])
        // console.log(result)
        if(result.code!=200)return  Toast.fail(result.msg);
        Toast('购物车中已取消');
          this.setData({
            ['shopData.shoppingCart']:false,
          })   
   }else{ //收藏
    let data = {
      spuId:this.data.shopData.id,
      addPrice:this.data.orderInfo.paymentPrice,
      quantity:this.data.orderInfo.quantity,
      spuName:this.data.shopData.name,
      size:this.data.orderInfo.size
     }
     let res= await getApp().api.postCollect(data)
     if(res.code!=200)return Toast.fail(res.msg);
    this.setData({
      ['shopData.shoppingCart']:true,
    })
    //修改状态
    wx.showToast({
      title: '添加成功',
      icon: 'none',
      duration: 2000
    })
   } 

},
async shopsTo(){
 let data =  {
    collector:"1",
    spuCode:this.data.shopData.id
   }
   let res = await getApp().api.postCarts(data)
   if(res.code!=200)return
   if(res.msg=="取消收藏成功"){
    this.setData({
      [`shopData.collect`]:false
    })
   }else if(res.msg=="收藏成功"){
    this.setData({
      [`shopData.collect`]:true
    })
   }  
   wx.showToast({
    title: res.msg,
    icon: 'none',
    duration: 2000
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})